package cn.gemiman.daoyi.module.cms.service.articlecategory;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.gemiman.daoyi.module.cms.controller.admin.articlecategory.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.articlecategory.CmsArticleCategoryDO;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;

import cn.gemiman.daoyi.module.cms.dal.mysql.articlecategory.CmsArticleCategoryMapper;

import static cn.gemiman.daoyi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gemiman.daoyi.module.cms.enums.ErrorCodeConstants.*;

/**
 * 文章栏目 Service 实现类
 *
 * @author 道一源码
 */
@Service
@Validated
public class CmsArticleCategoryServiceImpl implements CmsArticleCategoryService {

    @Resource
    private CmsArticleCategoryMapper cmsArticleCategoryMapper;

    @Override
    public Long createArticleCategory(CmsArticleCategorySaveReqVO createReqVO) {
        // 插入
        CmsArticleCategoryDO articleCategory = BeanUtils.toBean(createReqVO, CmsArticleCategoryDO.class);
        cmsArticleCategoryMapper.insert(articleCategory);
        // 返回
        return articleCategory.getId();
    }

    @Override
    public void updateArticleCategory(CmsArticleCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateArticleCategoryExists(updateReqVO.getId());
        // 更新
        CmsArticleCategoryDO updateObj = BeanUtils.toBean(updateReqVO, CmsArticleCategoryDO.class);
        cmsArticleCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteArticleCategory(Long id) {
        // 校验存在
        validateArticleCategoryExists(id);
        // 删除
        cmsArticleCategoryMapper.deleteById(id);
    }

    private void validateArticleCategoryExists(Long id) {
        if (cmsArticleCategoryMapper.selectById(id) == null) {
            throw exception(ARTICLE_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public CmsArticleCategoryDO getArticleCategory(Long id) {
        return cmsArticleCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<CmsArticleCategoryDO> getArticleCategoryPage(CmsArticleCategoryPageReqVO pageReqVO) {
        return cmsArticleCategoryMapper.selectPage(pageReqVO);
    }

}
